package com.sjq.common.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.sjq.common.dao.GenericDao;
import com.sjq.common.dao.MenuManagerDao;
import com.sjq.common.entity.Menu;
import com.sjq.common.util.PrimaryKeyUtil;
/**
 * 
 * @author xc
 * @version 2018.02.07
 * @since JDK1.6
 * 
 */
@Repository
public class MenuManagerDaoImpl extends GenericDao<Menu> implements MenuManagerDao {
	
	@Override
	public List<Menu> findMenuList() {
		String sql=" select xxzjbh,xt_id,cd_id,cdmc,wjcclj,cdcs,fcd_id,cdpx,cdms from T_PZ_SYS_MENU where djbgbs<>'3' order by cd_id ";
		 List<Menu> menuList=  getList(sql);
		return menuList;
	}
	//公共查询list方法
	public List<Menu> getList(String sql){
		List<Menu> menuList = this.getJdbcTemplate().query(sql,  new RowMapper<Menu>() {
            @Override
            public Menu mapRow(ResultSet resultSet, int i) throws SQLException {
            	 Menu menu = new Menu();
            	 menu.setXxzjbh(resultSet.getString("xxzjbh"));
            	 menu.setSystemId(resultSet.getString("xt_id"));  
                 menu.setMenuId(resultSet.getString("cd_id"));
                 menu.setMenuName(resultSet.getString("cdmc"));
                 menu.setMenuIconUrl(resultSet.getString("wjcclj"));
                 menu.setMenuUrl(resultSet.getString("cdcs"));
                 menu.setParentMenuId(resultSet.getString("fcd_id"));
                 menu.setOrderno(resultSet.getString("cdpx"));
                 menu.setCdms(resultSet.getString("cdms"));
                 return menu;
            }
        });
		return menuList;
	}
	//公共查询menu方法
	public Menu getMenu(String sql,Object[] param){
		
		Menu menu=this.getJdbcTemplate().query(sql, param, new ResultSetExtractor<Menu>(){

			@Override
			public Menu extractData(ResultSet resultSet) throws SQLException,
					DataAccessException {
				 Menu m = new Menu();
				while(resultSet.next()){
					 m.setXxzjbh(resultSet.getString("xxzjbh"));
	            	 m.setSystemId(resultSet.getString("xt_id"));  
	                 m.setMenuId(resultSet.getString("cd_id"));
	                 m.setMenuName(resultSet.getString("cdmc"));
	                 m.setMenuIconUrl(resultSet.getString("wjcclj"));
	                 m.setMenuUrl(resultSet.getString("cdcs"));
	                 m.setParentMenuId(resultSet.getString("fcd_id"));
	                 m.setOrderno(resultSet.getString("cdpx"));
	                 m.setCdms(resultSet.getString("cdms"));
				}
                 return m;
			}
			
		});
		  return menu;
	}

	@Override
	public int saveMenu(Menu menu) {
		StringBuilder sql = new StringBuilder()
					        .append(" insert into T_PZ_SYS_MENU ")
					        .append(" (xxzjbh,xt_id,cd_id,fcd_id,cdmc,WJCCLJ,cdcs,cdpx,cdms,djdw_gajgjgdm,djr_yhm, djsj,djbgbs,cdcc )")
					        .append(" values('"+PrimaryKeyUtil.getUUID()+"',?,'"+PrimaryKeyUtil.getUUID()+"',?,?,?,?,?,?,?,?,sysdate,'1','1' ) ");
		Object[] params = new Object[]{
			menu.getSystemId(),
			menu.getParentMenuId(),
			menu.getMenuName(),
			menu.getMenuIconUrl(),
			menu.getMenuUrl(),
			menu.getOrderno(),
			menu.getCdms(),
			menu.getDjdw_gajgjgdm(),
			menu.getDjr_yhm()
		};
		
		int count=this.getJdbcTemplate().update(sql.toString(), params);
		return count;
		
	}

	@Override
	public List<Menu> findBySystemId(String systemId) {
		String sql=" select xxzjbh,xt_id,cd_id,cdmc,wjcclj,cdcs,fcd_id,cdpx,cdms from T_PZ_SYS_MENU where djbgbs<>'3' and xt_id='"+systemId+"' order by cdpx,cd_id ";
		 List<Menu> menuList=  getList(sql);
		return menuList;
	}

	@Override
	public List<Menu> findByParentMenuId(String systemId, String parentMenuId) {
		String sql=" select xxzjbh,xt_id,cd_id,cdmc,wjcclj,cdcs,fcd_id,cdpx,cdms from T_PZ_SYS_MENU where djbgbs<>'3' and xt_id='"+systemId+"' and fcd_id like '"+parentMenuId+"%'  order by cd_id,cdpx  ";
		List<Menu> menuList=  getList(sql);
		return menuList;
	}

	@Override
	public Menu findByXxzjbh(String xxzjbh) {
		String sql=" select xxzjbh,xt_id,cd_id,cdmc,wjcclj,cdcs,fcd_id,cdpx,cdms from T_PZ_SYS_MENU where djbgbs<>'3' and xxzjbh=? order by cdpx,cd_id  ";
		
		Menu menu= getMenu(sql,new Object[]{xxzjbh});
		return menu;
	}
	@Override
	public int updateMenu(Menu menu) {

		StringBuilder sql = new StringBuilder()
					        .append(" update T_PZ_SYS_MENU ")
					        .append(" set (cdmc,WJCCLJ,cdcs,cdpx,cdms,djdw_gajgjgdm,djr_yhm,djbgsj,djbgbs,cdcc )")
					        .append(" =( select ?,?,?,?,?,?,?,sysdate,'2','1'  from dual ) where xxzjbh=? ");
		Object[] params = new Object[]{
			
			menu.getMenuName(),
			menu.getMenuIconUrl(),
			menu.getMenuUrl(),
			menu.getOrderno(),
			menu.getCdms(),
			menu.getDjdw_gajgjgdm(),
			menu.getDjr_yhm(),
			menu.getXxzjbh()
		};
		
		int count=this.getJdbcTemplate().update(sql.toString(), params);
		return count;
		
	}
	@Override
	public int deleteMenu(String xxzjbh) {
		//将登记变更标识 更新为3
		String sql=" update   T_PZ_SYS_MENU set (djbgsj,djbgbs) =(select sysdate,'3' from dual) where xxzjbh=? ";
		int i=this.getJdbcTemplate().update(sql, new String[]{xxzjbh});
		return i;
	}
	
	@Override
	public String updateDropRootNode(String xxzjbh, String parentMenuId) {
		String sql_getCdid=" select GET_CDBM('"+parentMenuId+"') cdid from dual ";
		Map<String, Object> cdMap=this.getJdbcTemplate().queryForMap(sql_getCdid);
		String sql=" update   T_PZ_SYS_MENU set (cd_id,fcd_id) =(select '"+cdMap.get("cdid").toString()+"','"+parentMenuId+"' from dual) where xxzjbh='"+xxzjbh+"' ";
		int c=this.getJdbcTemplate().update(sql);
		if(c>0){
			return cdMap.get("cdid").toString();
		}else{
			return "";
		}
		
	}
	@Override
	public int updateDropChildsNode(String dropChildNodesOldParentMenuId,
			String dropChildNodesNewParentMenuId,String systemId) {
		String sql=" update  T_PZ_SYS_MENU set (cd_id,fcd_id)"
				+" = ( select replace(cd_id,'"+dropChildNodesOldParentMenuId+"' , "
				+" 							'"+dropChildNodesNewParentMenuId+"'    )   , "
				+" 			replace(fcd_id,'"+dropChildNodesOldParentMenuId+"' , "
				+" 							'"+dropChildNodesNewParentMenuId+"'    ) "	
				+"    	from dual " 
				+"   ) where fcd_id like '"+dropChildNodesOldParentMenuId+"%' ";
		return this.getJdbcTemplate().update(sql) ;
	}
	@Override
	public int[] updateDropOrderNo(List<Object[]> bathArgs) {
		
		String sql=" update T_PZ_SYS_MENU set cdpx =? where xxzjbh =? ";
		return this.getJdbcTemplate().batchUpdate(sql, bathArgs);
	}


}
